package com.ruoyi.web.controller.crm;

import java.util.List;

import com.ruoyi.system.domain.CrmFollowUpVo;
import com.ruoyi.system.domain.CrmStudent;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CrmFollowUp;
import com.ruoyi.system.service.ICrmFollowUpService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 跟进记录Controller
 * 
 * @author Bill
 * @date 2024-06-24
 */
@Controller
@RequestMapping("/system/student/followup")
public class CrmFollowUpController extends BaseController
{
    private String prefix = "system/followup";

    @Autowired
    private ICrmFollowUpService crmFollowUpService;

    @RequiresPermissions("system:followup:view")
    @GetMapping()
    public String followup()
    {
        return prefix + "/followup";
    }

    @RequiresPermissions("system:followup:view")
    @GetMapping("/studentId/{id}")
    public String followupByStudentId(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("student", crmFollowUpService.selectCrmStudentById(id));

        return prefix + "/followup";
    }

    /**
     * 查询跟进记录列表
     */
    @RequiresPermissions("system:followup:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CrmFollowUp crmFollowUp)
    {
        startPage();
//        List<CrmFollowUpVo> list = crmFollowUpService.selectCrmFollowUpVoList(crmFollowUp);
//        return getDataTable(list);

        List<CrmFollowUp> list = crmFollowUpService.selectCrmFollowUpList(crmFollowUp);
        TableDataInfo rspData = getDataTable(list);
        crmFollowUpService.selectCrmFollowUpVoList(rspData);
        return rspData;
    }

    /**
     * 导出跟进记录列表
     */
    @RequiresPermissions("system:followup:export")
    @Log(title = "跟进记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CrmFollowUp crmFollowUp)
    {
        List<CrmFollowUp> list = crmFollowUpService.selectCrmFollowUpList(crmFollowUp);
        ExcelUtil<CrmFollowUp> util = new ExcelUtil<CrmFollowUp>(CrmFollowUp.class);
        return util.exportExcel(list, "跟进记录数据");
    }

    /**
     * 新增跟进记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增跟进记录
     */
    @GetMapping("/add/{id}")
    public String addByStudentId(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("studentId", id);
        return prefix + "/add";
    }
    /**
     * 新增保存跟进记录
     */
    @RequiresPermissions("system:followup:add")
    @Log(title = "跟进记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CrmFollowUp crmFollowUp)
    {
        crmFollowUp.setFollowUpId(getUserId());
        crmFollowUp.setCreateUser(getUserId());
        return toAjax(crmFollowUpService.insertCrmFollowUp(crmFollowUp));
    }

    /**
     * 修改跟进记录
     */
    @RequiresPermissions("system:followup:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CrmFollowUpVo crmFollowUp = crmFollowUpService.selectCrmFollowUpVoById(id);
        mmap.put("crmFollowUp", crmFollowUp);
        return prefix + "/edit";
    }

    /**
     * 修改保存跟进记录
     */
    @RequiresPermissions("system:followup:edit")
    @Log(title = "跟进记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CrmFollowUp crmFollowUp)
    {
        crmFollowUp.setUpdateUser(getUserId());
        return toAjax(crmFollowUpService.updateCrmFollowUp(crmFollowUp));
    }

    /**
     * 删除跟进记录
     */
    @RequiresPermissions("system:followup:remove")
    @Log(title = "跟进记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(crmFollowUpService.deleteCrmFollowUpByIds(ids));
    }
}
